//define(['js/module.js'], function(controllers) {
	'use strict';
	angular.module('backcenter.orgSort', [])
	.controller('orgSortCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, BizData, $uibModalInstance, params, title) {


   		var Model = this;
   		

   		/**
   		name:bizData#onBeforeDelete
   		description: <b>[回调型事件]</b>业务数据删除前
   		@param {object} event 它的结构如下:<br/>
   		{
   			"source":组件的js对象
   			,"cancel":可修改	，设置为true后中断当前delete动作
   			,"deleteIDList":删除的行Id数组
   		}
   		*/
   		Model.dataOrgBeforeDelete = function(event) {
//                               			alert(event.deleteIDList[0]);
//                               			event.cancel = true;
   		}
   		/**
   		name:bizData#onAfterDelete
   		description: <b>[回调型事件]</b>业务数据删除后
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   			"source" : 组件的js对象,
   			"deleteIDList" : 删除的行Id数组
   		}
   		</xmp>	
   		*/
   		Model.dataOrgAfterDelete = function(event) {
//                               			alert('删除之后！')
   		}

   		
   		
   		/**
   		name:bizData#onIndexChanging
   		description: <b>[回调型事件]</b>行记录变化中
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   			"source" : 组件的js对象,
   			"rowID" : 行Id,
   			"rowIndex" : 行索引,
   			"originalRowID" : 原行Id,
   			"originalRowIndex" : 原行索引
   		}
   		</xmp>	
   		*/
   		Model.dataOrgIndexChanging = function(event){
   		};
   		
   		
   		/**
   		name:bizData#onIndexChanged
   		description: <b>[回调型事件]</b>行记录变化
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   		"source" : 组件的js对象,
   		"rowID" : 行Id,
   		"rowIndex" : 行索引
   		}
   		</xmp>	
   		*/
   		Model.dataOrgIndexChanged = function(event) {
   		}
   		
   		/**
   		name:bizData#onDataChanged
   		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   		"source" : 组件的js对象,
   		"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
   		"selfChanged" : 是否自身变化，false：从data触发的变化,
   		"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
   		}
   		</xmp>	
   		*/
   		Model.dataOrgDataChanged = function(event) {
   		}
   		
   		/**
   		name:bizData#onValueChanged
   		description: <b>[回调型事件]</b>数据变化
   		@param {object} event 
   		<br/><b>结构如下：</b>
   		<xmp> 
   		{
   		"source" : 组件的js对象,
   		"column" : 关系,
   		"rowIndex" : 行索引,
   		"value" : 新值,
   		"originalValue" : 旧值
   		}
   		</xmp>	
   		*/
   		//输入值变化，监视input的输入，ng-change
   		Model.dataOrgValueChanged = function(event) {
			
   		}
   		


   		/**
   		name:bizData#onBeforeNew
   		description: <b>[回调型事件]</b>业务数据新增前
   		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
   		*/
   		Model.dataOrgBeforeNew = function(event) {
			
   		};
   		
   		
   		/**
   		* name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
   		* 
   		* @param event
   		*            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
   		*/
   		Model.dataOrgAfterNew = function(event){

   		}
   		

   		
		var options = {};
		options.container = Model;
		options.concept = 'SaOporg';
		options.name = 'dataOrg';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/backcenter/org/tree/create';
		options.reader = '/backcenter/org/tree/query';
		options.writer = '/backcenter/org/tree/save';
		options.deleter = '/backcenter/org/tree/delete';
		options.idColumnDefine = "sID";
		options.idColumnName = "sID";
//		options.defCols = {sID:{name:"sID",type:"String",label:""},sName:{name:"sName",type:"String",label:""},sCode:{name:"sCode",type:"String",label:""},sLongName:{name:"sLongName",type:"String",label:""},sFName:{name:"sFName",type:"String",label:""},sFCode:{name:"sFCode",type:"String",label:""},sFID:{name:"sFID",type:"String",label:""},sOrgKindID:{name:"sOrgKindID",type:"String",label:""},sSequence:{name:"sSequence",type:"String",label:""},sValidState:{name:"sValidState",type:"Integer",label:""},sParent:{name:"sParent",type:"String",label:""},sNodeKind:{name:"sNodeKind",type:"String",label:""},sLevel:{name:"sLevel",type:"Integer",label:""},sPhone:{name:"sPhone",type:"String",label:""},sFax:{name:"sFax",type:"String",label:""},sAddress:{name:"sAddress",type:"String",label:""},sZip:{name:"sZip",type:"String",label:""},sDescription:{name:"sDescription",type:"String",label:""},sPersonID:{name:"sPersonID",type:"String",label:""},sCreatorFID:{name:"sCreatorFID",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.defCols = {sID:{name:"sID",type:"String",label:""},sName:{name:"sName",type:"String",label:""},sCode:{name:"sCode",type:"String",label:""},sLongName:{name:"sLongName",type:"String",label:""},sFName:{name:"sFName",type:"String",label:""},sFCode:{name:"sFCode",type:"String",label:""},sFID:{name:"sFID",type:"String",label:""},sOrgKindID:{name:"sOrgKindID",type:"String",label:""},sSequence:{name:"sSequence",type:"String",label:""},sValidState:{name:"sValidState",type:"Integer",label:""},sParent:{name:"sParent",type:"String",label:""},sNodeKind:{name:"sNodeKind",type:"String",label:""},sLevel:{name:"sLevel",type:"Integer",label:""},sAddress:{name:"sAddress",type:"String",label:""},sAddressDetail:{name:"sAddressDetail",type:"String",label:""},contacts:{name:"contacts",type:"String",label:""},sPhone:{name:"sPhone",type:"String",label:""},sFax:{name:"sFax",type:"String",label:""},sMail:{name:"sMail",type:"String",label:""},sZip:{name:"sZip",type:"String",label:""},sDescription:{name:"sDescription",type:"String",label:""},sPersonID:{name:"sPersonID",type:"String",label:""},sCreatorFID:{name:"sCreatorFID",type:"String",label:""},images:{name:"images",type:"String",label:""},summary:{name:"summary",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = true;
		options.parentRelation = 'sParent';
		options.nodeKindRelation = 'sNodeKind';
		options.nodeLevelRelation = 'sLevel';
		options.nodeSortRelation = "sSequence";
		options.pathRelation = 'sFID';
		options.directDeleteMode = false;
   		options.limit = -1;
		options.orderBy = 'sSequence ASC';

   		options.onIndexChanging = Model.dataOrgIndexChanging;
   		options.onIndexChanged = Model.dataOrgIndexChanged;
   		options.onBeforeNew = null;
   		options.onAfterNew = Model.dataOrgAfterNew;
   		options.onBeforeDelete = null;
   		options.onAfterDelete = Model.dataOrgAfterDelete;
   		options.onValueChanged = Model.dataOrgValueChanged;
   		options.onDataChanged = Model.dataOrgDataChanged;
//                               		options.rowsWatcherFn = Model.rowsWatcherFn;
   		
   		$scope.dataOrg = new BizData(options);
//                               		$scope.dataOrg.refreshData();

//                               		$scope.dataOrg.deregIndex = $scope.$watch('dataOrg.currentIndex',$scope.dataOrg.watchCurrentIndex);
//                               		$scope.dataOrg.deregID = $scope.$watch('dataOrg.currentID',$scope.dataOrg.watchCurrentID);
   		//监视对象中的所有变动
   		$scope.dataOrg.deregRows = $scope.$watch('dataOrg.rows',$scope.dataOrg.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
//                               		$scope.dataOrg.deregCollection = $scope.$watch('dataOrg.rows',$scope.dataOrg.watchCollection,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
   		//////////////////////////////////////////////


		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
            Model.title = title;  
            Model.params = params;  

            if(Model.params.parentID==null){
        		//设置过滤条件
        		$scope.dataOrg.filter = "SaOporg.sCreatorFID='" + Model.params.id + "' AND SaOporg.sParent is null";//
            }else{
        		//设置过滤条件
        		$scope.dataOrg.filter = "SaOporg.sCreatorFID='" + Model.params.id + "' AND SaOporg.sParent='"+ Model.params.parentID +"'";//
            }
      	
       		$scope.dataOrg.refreshData();

        });  
		
        //置顶
        Model.btnMoveTopClick = function () {
        	var rowID = $scope.dataOrg.getCurrentID();
        	var index = $scope.dataOrg.getIndex(rowID);
        	if(rowID)
        	$scope.dataOrg.moveRowPro(0 - index, rowID);
        }
        //上移
        Model.btnMoveUpClick = function () {
        	var rowID = $scope.dataOrg.getCurrentID();
        	if(rowID)
        	$scope.dataOrg.moveRowPro(-1, rowID);
        }
        //下移
        Model.btnMoveDownClick = function () {
        	var rowID = $scope.dataOrg.getCurrentID();
        	if(rowID)
        	$scope.dataOrg.moveRowPro(1, rowID);
        }
        //置底
        Model.btnMoveBottomClick = function () {
        	var rowID = $scope.dataOrg.getCurrentID();
        	var index = $scope.dataOrg.getIndex(rowID);
        	var count = $scope.dataOrg.getTotal();
        	if(rowID)
        	$scope.dataOrg.moveRowPro(count - index - 1, rowID);
        }
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
			for(var index=0;index<$scope.dataOrg.rows.length;index++){
				var rowID = $scope.dataOrg.rows[index].sID.value;
				data.push(rowID);
			}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
		//////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
//});
